<?php
// 购物车数据
// 返回数据
// [
//    shop:{}
//    cart:[...]
//    product:[...]
// ]
//
//
require_once $this->website['path']['action'] . '/app/token.php';

$json = array('status' => 'error', 'msg' => '', 'data' => '');
$uid = $basic['uid'];

$cart = $this->website['class']['db']
	->table('product_cart')
	->where('uid=? and visbile=1', $uid)
	->order('id desc')
	->select();

$data = [];
foreach ($cart as $v) {
	$product = $this->website['class']['db']
		->table('product')
		->where('id=?', $v['pid'])
		->order('id desc')
		->one();
	if (!($product['id'] ?? false)) {
		continue;
	}
	if ($data[$v['sid']]) {
		$data[$v['sid']]['cart'][] = $v;
		$data[$v['sid']]['product'][] = $product;
	} else {
		$shop = $this->website['class']['db']
			->table('shops')
			->field('id', 'tit', 'image')
			->where('id=?', $v['sid'])
			->one();
		$data[$v['sid']] = array(
			'shop' => $shop,
			'cart' => array($v),
			'product' => array($product),
		);
	}
}
$data = array_column($data, null);

// 检测数据是否改动过
foreach ($data as &$v) {
	foreach ($v['cart'] as &$vv) {
		$param = json_decode($vv['param'], 1);
		$vv['checked'] = fn_price_cheked(
			$website,
			array(
				'price' => $vv['price'],
				'pid' => $vv['pid'],
				'param' => $param,
			)
		);
	}
}

$json['status'] = 'ok';
$json['data'] = $data;

$this->json_encode_encrypt($json);